from django.shortcuts import redirect,render,HttpResponse
from app_user import models
from app_user.models import UserInfo,Address,Order, Goods
from app_user.utls.form import user_address_add_ModeForm, edit_user_inforModeForm

import json




def user_list(request):
    """"""
    return render(request, 'test1.html')





def user_info(request):
    """"""
    """用户中心信息页面"""
    cookies = request.COOKIES.get('goods_cookies', '')
    visit_goods = []
    if cookies != "":
        visit_goods_id_list = cookies.split(";")
        for goods_id in visit_goods_id_list:
            if goods_id:
                visit_goods.append(Goods.objects.filter(id=goods_id).first())



    info = request.session.get('info')
    id = info['id']
    cart_num = models.Shopping_Cart.objects.filter(user_id=id).all().count()
    user_data = UserInfo.objects.filter(id=id).first()

    datas = {
        "user_data": user_data,
        "cart_num": cart_num,
        "visit_goods": visit_goods,
    }

    return render(request, "user_info.html", datas)




def user_info_edit(request):
    """"""
    """修改个人信息"""
    id = request.session.get('info')['id']
    user_data = UserInfo.objects.filter(id=id).first()
    cart_num = models.Shopping_Cart.objects.filter(user_id=id).all().count()

    if request.method == "GET":
        form = edit_user_inforModeForm(instance=user_data)
        data = {
            "form": form,
            "cart_num":cart_num,
        }
        return render(request, "uer_info_edit.html", context=data)

    form = edit_user_inforModeForm(data=request.POST, instance=user_data)
    if form.is_valid():
        form.save()
        return redirect("/user/info/")
    else:
        data = {
            "form": form,
            "cart_num": cart_num,
        }
        return render(request, "uer_info_edit.html", context=data)








def address(request):
    """"""
    """收货地址信息"""
    id = request.session.get('info')['id']
    cart_num = models.Shopping_Cart.objects.filter(user_id=id).all().count()

    address_data_default = Address.objects.filter(user_id=id, is_default=True).first()
    address_data_not_default = Address.objects.filter(user_id=id, is_default=False).all()
    data = {
        "address_data_default":address_data_default,
        "address_data_not_default":address_data_not_default,
        "cart_num": cart_num,
    }


    return render(request, "user_address.html", context=data)



def add_address(request):
    """"""
    """添加收货地址"""
    id = request.session.get('info')['id']
    cart_num = models.Shopping_Cart.objects.filter(user_id=id).all().count()
    if request.method == "GET":
        form = user_address_add_ModeForm()
        data = {
            "form": form,
            "cart_num": cart_num,
        }

        return render(request, 'add_address.html', context=data)

    form = user_address_add_ModeForm(data=request.POST)

    if form.is_valid():
       form.instance.user_id = request.session['info']['id']
       form.save()

       return redirect('/user/address/')

    else:
        data = {
            "form": form,
            "cart_num": cart_num,
        }
        return render(request, 'add_address.html', context=data)







def default_address(request):
    """"""
    """设置默认地址"""
    user_id = request.session.get('info')['id']
    address_id = request.GET.get('uid')

    models.Address.objects.filter(user_id=user_id, is_default=True).update(is_default=False)

    models.Address.objects.filter(id=address_id).update(is_default=True)

    data = {
        "status": True
    }
    return HttpResponse(json.dumps(data))

def delete_address(request):
    """"""
    """删除地址"""

    address_id = request.GET.get('uid')
    models.Address.objects.filter(id=address_id).delete()

    data = {
        "status": True,
    }

    return HttpResponse(json.dumps(data))




def cart(request):
    """"""
    """购物车"""
    id = request.session.get('info')['id']
    cart_num = models.Shopping_Cart.objects.filter(user_id=id).all().count()

    cart_data = models.Shopping_Cart.objects.filter(user_id=id).all()
    cart_datas = []
    total = 0
    for item in cart_data:
        goods_obj = models.Goods.objects.filter(id=item.goods_id).first()
        total += item.total
        cart_datas.append((goods_obj,item))

    data = {
        "cart_data": cart_datas,
        "cart_num": cart_num,
        "cart_total": total,
    }

    return render(request, "cart.html", context=data)



def delete_cart(request, id):
    """"""
    """删除购物车"""

    models.Shopping_Cart.objects.filter(id=id).delete()

    return redirect("/cart/")